package com.ews.onlineexchange.controller;

import java.util.ArrayList;
import java.util.List;

import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.ChooseType;
import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.TradeStatus;
import com.ews.onlineexchange.service.EntityQuery;

@Model
public class AuditBestChooseList extends EntityQuery<Bulletin> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public AuditBestChooseList() {
		setMaxResults(MAX_RESULTS_NORMAL);
		setOrder("createDate DESC");
	}

	@Override
	protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<Bulletin> entity) {
		List<Predicate> restrictions = new ArrayList<Predicate>();
		restrictions.add( cb.equal( entity.get( "delFlag" ), DataEntity.DEL_FLAG_NORMAL ) );
		restrictions.add( cb.isTrue( entity.<Boolean>get( "auditResult" ) ) );
		restrictions.add( cb.equal( entity.get( "wayOfChoose" ), ChooseType.Best ) );
		restrictions.add( cb.equal( entity.get( "tradeStatus" ), TradeStatus.Waiting ) );
		return restrictions;
	}

	@Override
	protected void fetch(Root<Bulletin> entity) {
		super.fetch(entity);
	}
	
	@Inject
	EntityManager entityManager;
	private String QUERY_UNAUDIT_BEST_CHOOSE_BULLETIN_EQL = ""
			+ "		SELECT 		bulletin "
			+ "		FROM 		Bulletin bulletin "
			+ "		WHERE 		auditResult = 1 "
			+ "		AND 		tradeStatus = 'Waiting' "
			+ "		AND			wayOfChoose = 'Best' "
			+ "		AND 		delFlag = '0' "
			+ "		ORDER BY 	createDate DESC";
	private int MAX_SIZE = 7;		
	public List<Bulletin> getUnauditUnaduitBestBulletins(){
		return entityManager.createQuery(QUERY_UNAUDIT_BEST_CHOOSE_BULLETIN_EQL).setMaxResults(MAX_SIZE).getResultList();
	}
	

}
